<template>
  <div class="box">
    <el-input
      v-model="input"
      placeholder="根据用户名搜索"
      size="small"
    ></el-input>
    <el-row>
      <el-button size="small" @click="empty">清空</el-button>
      <el-button type="primary" size="small" @click="search">搜索</el-button>
      <el-button type="success" size="small" icon="el-icon-edit" class="added" @click="added"
        >新增用户</el-button
      >
    </el-row>
    <div class="record">
      <i class="el-icon-info"></i>
      <span>共 {{ counts }} 条记录</span>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="序号" width="180" align="center">
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="180" align="center">
      </el-table-column>
      <el-table-column prop="phone" label="联系电话" align="center">
      </el-table-column>
      <el-table-column prop="username" label="用户名" align="center">
      </el-table-column>
      <el-table-column
        prop="permission_group_title"
        label="权限名称"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="role" label="角色" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            plain
            circle
            @click="handleEdit(scope.row)"
            icon="el-icon-edit"
            v-show="scope.row.username !=='超级管理员'"
          ></el-button>
          <el-button
            size="mini"
            type="danger"
            plain
            circle
            @click="handleDelete(scope.row)"
            icon="el-icon-delete"
            v-show="scope.row.username !=='超级管理员'"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-button
      v-show="fullscreenLoading"
      type="primary"
      v-loading.fullscreen.lock="fullscreenLoading"
    >
    </el-button>
    <div class="butt">
      <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage4"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="10"
      layout="  prev, pager, next, sizes, jumper"
      :total="counts"
      style="margin-right: -200px;"
    >
    </el-pagination>
    </div>
    <usersAdd ref="usersAdd" :text="text" :simpl="simpl" :details="details"></usersAdd>
  </div>
</template>

<script>
import { list, remove } from '@/api/base/users.js'
import { simple } from '@/api/base/permissions.js'
import usersAdd from '../components/user-add.vue'
export default {
  components: {
    usersAdd
  },
  data () {
    return {
      simpl: [],
      details: {},
      text: '',
      currentPage4: 1,
      fullscreenLoading: false,
      data: {
        page: 1,
        pagesize: 10
      },
      counts: 1,
      input: '',
      tableData: [
        {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        },
        {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        },
        {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄'
        }
      ]
    }
  },

  async created () {
    this.userLis()
  },

  methods: {
    // 修改数据
    async handleEdit (row) {
      // const data = await detail(row)
      this.details = row
      this.text = '编辑用户'
      this.$refs.usersAdd.dialogFormV()
      const res = await simple()
      this.simpl = res.data
      this.$refs.usersAdd.updata()
    },

    // 添加用户
    async added () {
      this.$refs.usersAdd.dialogFormV()
      this.text = '创建用户'
      const data = await simple()
      this.simpl = data.data
      this.$refs.usersAdd.formBase = {
        avatar: '',
        role: '',
        phone: '',
        permission_group_id: '',
        email: '',
        password: '',
        username: '',
        introduction: ''
      }
    },
    async handleDelete (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        remove(row)
        this.userLis()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      // this.userLis()
    },
    async userLis () {
      this.fullscreenLoading = true
      const { data } = await list(this.data)
      this.tableData = data.list
      this.counts = data.counts
      this.fullscreenLoading = false
    },
    handleClick (row) {
      console.log(row)
    },
    handleSizeChange (val) {
      this.data.page = 1
      this.data.pagesize = val
      this.userLis()
    },
    handleCurrentChange (val) {
      this.data.page = val
      this.userLis()
    },
    // 清空
    empty () {
      this.input = ''
      this.userLis()
    },
    // 搜索
    async search () {
      const { data } = await list(this.data)
      this.tableData = data.list.filter(item => item.username === this.input)
    }
  }
}
</script>

<style scoped lang="less">
.butt{
  margin: 20px;
  text-align: right;
  margin-right: 220px;
  padding-bottom: 20px;
}
.el-table {
  padding: 0 20px;
}
.record {
  width: 1260px;
  height: 40px;
  border-radius: 4px;
  line-height: 40px;
  margin: 20px auto;
  background-color: #f4f4f5;
  font-size: 14px;
  color: #ccc;
  span {
    margin-left: 10px;
  }
  .el-icon-info {
    font-size: 15px;
    color: #909399;
    margin-left: 15px;
  }
}
.added {
  margin-left: 825px;
}
.box {
  width: 1300px;
  // height: 600px;
  margin: 20px 20px;
  border-radius: 5px;
  background-color: #fff;
}
.el-input {
  width: 200px;
  margin-top: 20px;
  margin-left: 20px;
}
.el-row {
  margin-top: -32px;
  margin-left: 235px;
}
</style>
